﻿layui.config({
    base: '/Lib/layui/extend/'
}).extend({
    treeGrid: 'treeGrid'
}).use(['form', 'layer', 'table', 'laypage', 'laytpl', 'laydate', 'tree', 'treeGrid', 'jquery'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        laydate = layui.laydate,
        player = parent.layer || layui.layer,
        tree = layui.tree,
        treeGrid = layui.treeGrid; //很重要

    var authorityRoleAuthorizeLogic = {
        //初始化
        init: function () {

            self = this;

            this.Parameters = {
                queryData: undefined,
                index: undefined,
                field: 'iSort',
                order: 'asc',
                rowData: undefined,//选中行信息
                menusListsUrl: "/Authority/Role/LoadMenusListInfo",
                menuButtonListByRoleMenuCodeUrl: "/Authority/Role/LoadMenuButtonListByRoleMenuCode",
                modifyRoleMenuButtonInfoUrl: "/Authority/Role/ModifyRoleMenuButtonInfo",
                modifyRoleMenuInfoUrl: "/Authority/Role/ModifyRoleMenuInfo",
            };

            this.initPage();

            this.bindEvent();

        },
        //初始化列表
        initPage: function () {
            //获取信息
            self.Parameters.queryData = GrdAssist.queryString("data");
            self.logicFunc.loadListInfo();
        },
        //绑定事件
        bindEvent: function () {

            //监听提交
            form.on('submit(authorizeButton)', self.logicFunc.saveInfo);

            //关闭
            $("#btnClose").on("click", self.logicFunc.close);

            //监听(列表操作)
            treeGrid.on('tool(menusList)', function (obj) {
                var data = obj.data;
                self.Parameters.rowData = data;
                //选择按钮
                if (obj.event === 'funBtnButtons') {
                    self.logicButtonsFunc.selectButtons();
                }
            });

            ////////////////////////////////////////////////////////////////
            //监听提交
            form.on('submit(selestButtons)', self.logicButtonsFunc.saveInfo);

            //关闭
            $("#btnSelestClose").on("click", self.logicButtonsFunc.close);

        },
        //逻辑方法
        logicFunc: {

            //获取数据
            loadListInfo: function () {
                var treeTable = treeGrid.render({
                    elem: '#menusList',
                    url: self.Parameters.menusListsUrl,
                    where: {
                        parameters: GrdLayUI.zip(self.Parameters.queryData)
                    },
                    cellMinWidth: 100,
                    height: "full-90",
                    idField: 'gCode',//必須字段
                    treeId: 'gCode',//树形id字段名称
                    treeUpId: 'gParentCode',//树形父id字段名称
                    treeShowName: 'sName',//以树形式显示的字段
                    isOpenDefault: true,//节点默认是展开还是折叠【默认展开】
                    cheDisabled: true,
                    page: false,
                    cols: [[
                        { type: 'checkbox' },
                        {
                            field: 'sName', title: '菜单名称', width: '180',
                            templet: function (d) {
                                return '<a><i class="iconfont ' + d.sIconFont + '" data-icon="' + d.sIconFont + '" ></i><cite>' + d.sName + '</cite></a>';
                            }
                        },
                        { field: 'sUrl', title: '链接地址', width: '230' },
                        { field: 'iSort', title: '排序', width: '80', align: "center" },
                        { title: '操作', toolbar: '#gridTool', width: '120', align: 'left' }//fixed: 'right', 
                    ]],
                    onCheck: function (obj) {
                        self.Parameters.rowData = obj;
                    }
                });
            },

            //保存数据
            saveInfo: function () {

                var roleCode = self.Parameters.queryData;
                var parametersList = new Array();　//创建一个数组

                var selectMenusListInfo = treeGrid.treeFindRowCheck("menusList");

                $.each(selectMenusListInfo, function (n, value) { 
                    parametersList.push(value.gCode);
                });  

                var json = {
                    parameters: {
                        gMenuCodeList: parametersList,
                        gRoleCode: roleCode
                    }
                };
                GrdLayUI.ajaxPost({
                    url: self.Parameters.modifyRoleMenuInfoUrl,
                    data: json,
                    success: function (result) {
                        if (result.Code == 1000) {
                            GrdLayUI.msg(result.Msg, "成功", function () {
                                //获取窗口索引
                                var index = player.getFrameIndex(window.name);
                                //关闭子页面
                                player.close(index);
                                //刷新父页面
                                window.parent.location.reload();
                            });
                        } else {
                            GrdLayUI.msg(result.Msg, "警告");
                        }
                    }
                });
            },

            //取消
            close: function () {
                player.closeAll();
            }
        },
        logicButtonsFunc: {

            //选择按钮
            selectButtons: function () {
                var json = {
                    parameters: {
                        roleCode: self.Parameters.queryData,
                        menuCode: self.Parameters.rowData.gCode
                    }
                };
                GrdLayUI.ajaxPost({
                    url: self.Parameters.menuButtonListByRoleMenuCodeUrl,
                    data: json,
                    success: function (result) {
                        if (result.Code == 1000) {

                            var resultData = result.Data;

                            var htmls = "";
                            $(".layui-form-buttons").empty();
                            for (var i = 0; i < resultData.length; i++) {
                                htmls +=
                                    `<div class="grouping">
                                        <fieldset class="layui-elem-field layui-field-title" style = "margin-top: 20px;" >
                                        <legend>
                                            <input type="hidden" class="layui-input" name="gCode" value="`+ resultData[i].gCode + `">
                                            <input type="hidden" class="layui-input" name="gRoleButtonCode" value="`+ resultData[i].gRoleButtonCode + `">
                                            <input type="hidden" class="layui-input" name="gMenuButtonCode" value="`+ resultData[i].gMenuButtonCode + `">
                                            `+ resultData[i].sHtmlXs + `
                                            <input type="checkbox" lay-skin="primary" title="启用id为：`+ resultData[i].sId + `的按钮" ` + (resultData[i].gRoleButtonCode != null && resultData[i].gRoleButtonCode != "" && resultData[i].gRoleButtonCode != "NULL" ? 'checked' : '') + `  name="gCodeCheckbox" >
                                        </legend>
                                        </fieldset>

                                        <div class="layui-form-item">
                                            <div class="layui-inline">
                                                <label class="layui-form-label" style="width:60px">类型：</label>
                                                <div class="layui-input-block">
                                                    <input type="radio" name="`+ resultData[i].gCode + `iType" value="0" title="普通" ` + (resultData[i].iType == null || resultData[i].iType == 0 ? 'checked' : '') + `>
                                                    <input type="radio" name="`+ resultData[i].gCode + `iType" value="1" title="缩小" ` + (resultData[i].iType != null && resultData[i].iType == 1 ? 'checked' : '') + `>
                                                    <input type="radio" name="`+ resultData[i].gCode + `iType" value="2" title="都有" ` + (resultData[i].iType != null && resultData[i].iType == 2 ? 'checked' : '') + `>
                                                </div>
                                            </div>
                                            <div class="layui-inline">
                                                <label class="layui-form-label" style="width:60px">排序：</label>
                                                <div class="layui-input-inline">
                                                   <input type="text" name="iSort" autocomplete="off" class="layui-input"  value="`+ (resultData[i].iSort != null ? resultData[i].iSort : '') + `">
                                                </div>
                                            </div>
                                        </div>

                                        <div class="layui-form-item">
                                            <label class="layui-form-label" style="width:60px">缩小：</label>
                                            <div class="layui-input-inline" style="width: 540px;">
                                                <textarea placeholder="请输入缩小按钮特殊处理脚本" name="sMinBtnRemark" style="min-height:50px" class="layui-textarea">`+ (resultData[i].sMinBtnRemark != null ? resultData[i].sMinBtnRemark : '') + `</textarea>
                                            </div>
                                        </div>
                                    </div>`;
                            }
                            $(".layui-form-buttons").html(htmls);
                            //自定页
                            self.Parameters.index = layer.open({
                                title: "菜单【" + self.Parameters.rowData.sName + "】选择按钮",
                                type: 1,
                                skin: 'layui-layer-demo', //样式类名
                                area: ['700px', '650px'],
                                closeBtn: 1, //0不显示关闭按钮 1显示关闭按钮 
                                anim: 2,
                                content: $('#buttons-form')
                            });
                            form.render(); //渲染页面

                        } else {
                            GrdLayUI.msg(result.Msg, "警告");
                        }
                    }
                });
            },

            //保存数据
            saveInfo: function () {

                var parametersList = new Array();　//创建一个数组

                var roleCode = self.Parameters.queryData;
                var menuCode = self.Parameters.rowData.gCode;

                $(".grouping input[type=checkbox]").each(function () {
                    if (this.checked) {
                        var parentInfo = $(this).parent().parent().parent();
                        var gCode = parentInfo.find("input[name='gCode']").val();
                        var gRoleButtonCode = parentInfo.find("input[name='gRoleButtonCode']").val();
                        var iType = parentInfo.find('input[name=' + gCode + 'iType]:checked').val();
                        var iSort = parentInfo.find("input[name='iSort']").val();
                        var sMinBtnRemark = parentInfo.find("textarea[name='sMinBtnRemark']").val();
                        var gMenuButtonCode = parentInfo.find("input[name='gMenuButtonCode']").val();

                        if (iSort == null || iSort == "") {
                            GrdLayUI.msg("排序不能为空！", "警告");
                            return;
                        }
                        var item = {
                            gCode: gRoleButtonCode,
                            gRoleCode: roleCode,
                            gMenuCode: menuCode,
                            gMenuButtonCode: gMenuButtonCode,
                            iSort: iSort,
                            iType: iType,
                            sMinBtnRemark: sMinBtnRemark
                        };
                        parametersList.push(item);
                    }
                });
                var json = {
                    parameters: {
                        roleMenuButtonModifyInfo: parametersList,
                        gRoleCode: roleCode,
                        gMenuCode: menuCode
                    }
                };
                GrdLayUI.ajaxPost({
                    url: self.Parameters.modifyRoleMenuButtonInfoUrl,
                    data: json,
                    success: function (result) {
                        if (result.Code == 1000) {
                            GrdLayUI.msg(result.Msg, "成功", function () {
                                layer.close(self.Parameters.index);
                            });
                        } else {
                            GrdLayUI.msg(result.Msg, "警告");
                        }
                    }
                });
            },
            //取消button页
            close: function () {
                //关闭当前页 
                layer.close(self.Parameters.index);
            }
        },

        //工具
        tools: {
        }
    };
    authorityRoleAuthorizeLogic.init();
});
